package com.youfang.sixthmedia;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzit.common.async.AsynCallback;
import com.gzit.utils.AndroidUtils;
import com.youfang.sixthmedia.common.YouFangUtils;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.message.GetuserbyusernameRequest;
import com.youfang.sixthmedia.message.GetuserbyusernameResponse;
import com.youfang.sixthmedia.message.LoginRequest;
import com.youfang.sixthmedia.message.LoginResponse;
import com.youfang.sixthmedia.message.center.AddFriendRequest;
import com.youfang.sixthmedia.message.center.AddFriendResponse;
import com.youfang.sixthmedia.message.center.RemoveFriendRequest;
import com.youfang.sixthmedia.message.center.RemoveFriendResponse;
import com.youfang.sixthmedia.message.model.Buddy;
import com.youfang.sixthmedia.message.model.User;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;

public class BuddyDetailActivity extends Activity {

	private Buddy buddy;
	private boolean isBuddy;

	private void setupTestStub() {

		buddy = new Buddy();
		buddy.userid = 552;
		buddy.username = "zhansheng";
		buddy.nickname = buddy.username;

		LoginRequest login = new LoginRequest();
		login.setUsername("teddy");
		login.setPassword("443260");
		Net.doHttpRemote(this, login, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("登录失败");
							return;
						}

						// 登录成功...
						LoginResponse resp = (LoginResponse) r;
						User user = resp.getUser();

						// save user infomation
						user.password = "443260";
						YoufangContext.saveUserAndStartMessageService(user);
						remoteLoadBuddyDetail();

					}
				});

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.buddy_detail_activity);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		buddy = (Buddy) bundle.getSerializable("data");
		remoteLoadBuddyDetail();

		// test...
//		setupTestStub();

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.chatbtn:

					Intent intent = new Intent();
					intent.setClass(BuddyDetailActivity.this,
							ChatActivity.class);
					intent.putExtra("data", buddy);
					startActivity(intent);

					break;
				}

			}
		};
		this.findViewById(R.id.chatbtn).setOnClickListener(listener);

	}

	private void remoteLoadBuddyDetail() {
		GetuserbyusernameRequest req = new GetuserbyusernameRequest();
		req.setUsername(buddy.username);
		Net.doHttpRemote(this, req, new GetuserbyusernameResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("加载房友信息失败");
							return;
						}

						updateUI((GetuserbyusernameResponse) r);

					}
				});

	}

	private void updateFriendButton() {
		Button friendBtn = (Button) this.findViewById(R.id.friendbtn);
		friendBtn.setCompoundDrawablesWithIntrinsicBounds(
				this.getResources()
						.getDrawable(
								isBuddy ? R.drawable.friend_del
										: R.drawable.friend_add), null, null,
				null);

		friendBtn.setText(isBuddy ? "删除好友" : "添加好友");
	}

	private void updateUI(final GetuserbyusernameResponse resp) {

		((TextView) this.findViewById(R.id.name)).setText(resp.getNickname());
		((TextView) this.findViewById(R.id.distance)).setText(YouFangUtils
				.getDistanceDesc(buddy.distance));
		((TextView) this.findViewById(R.id.introduce)).setText(resp
				.getIntroduce());

		((ImageView) this.findViewById(R.id.header))
				.setImageURI(HeaderController.Cache
						.getFromCache(this.buddy.userid));

		// set buddy status icon
		isBuddy = resp.isIsbuddy();
		updateFriendButton();

		Button friendBtn = (Button) this.findViewById(R.id.friendbtn);
		friendBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isBuddy) { // trigger delete friend action

					RemoveFriendRequest req = new RemoveFriendRequest();
					req.setBuddyid(resp.getUserid());
					Net.doHttpRemote(BuddyDetailActivity.this, req,
							new RemoveFriendResponse(),
							new AsynCallback<Response>() {

								@Override
								public void callback(Response r) {
									if (!r.isOk()) {
										AndroidUtils.toastShort("删除好友失败,请稍后再试");
										return;
									}

									AndroidUtils.toastShort("删除好友成功");
									isBuddy = false;
									updateFriendButton();

								}
							});

					return;
				}

				// trigger add friend action
				AddFriendRequest req = new AddFriendRequest();
				req.setBuddyid(resp.getUserid());
				Net.doHttpRemote(BuddyDetailActivity.this, req,
						new AddFriendResponse(), new AsynCallback<Response>() {

							@Override
							public void callback(Response r) {
								if (!r.isOk()) {
									AndroidUtils.toastShort("添加好友失败,请稍后再试");
									return;
								}

								AndroidUtils.toastShort("添加好友成功");
								isBuddy = true;
								updateFriendButton();

							}
						});
			}
		});

	}
}
